package util;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;


/**
 * This class simply creates a bundle from a resource file that correspondes 
 * to the target class' name.  If more functionality from the enclosed bundle
 * is needed, more accessor methods will need to be written.
 * 
 * @author Robert C. Duvall
 * @author Alexander Schearer <aas11@duke.edu>
 */
public class ResourceManager implements Iterable<String>
{
    private ResourceBundle myResources;
    private final static String RESOURCE_DIR = "resources.";
    
    public ResourceManager (Object target)
    {
        myResources = ResourceBundle.getBundle(RESOURCE_DIR + getClassName(target));
    }
    
    public ResourceManager (String target)
    {
    	myResources = ResourceBundle.getBundle(RESOURCE_DIR + target);
    }

    public String getString (String key)
    {
        return myResources.getString(key);
    }
    
    public Iterator<String> iterator()
    {
    	List<String> l = new ArrayList<String>();
    	Enumeration<String> e = myResources.getKeys();
    	while (e.hasMoreElements())
    	{
    		l.add(e.nextElement());
    	}
    	return l.iterator();
    }
    
    private String getClassName (Object target)
    {
        String classname = target.getClass().getName();
        return classname.substring(classname.lastIndexOf(".") + 1);
    }
}
